package com.yuke.cloud.service.wmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.wmc.controller.frontend.WmsStorageController;
import com.yuke.cloud.service.wmc.dao.WmsStockInMapper;
import com.yuke.cloud.service.wmc.dao.WmsStockInOutMapper;
import com.yuke.cloud.service.wmc.dao.WmsStockOutMapper;
import com.yuke.cloud.service.wmc.dao.WmsStorageMapper;
import com.yuke.cloud.service.wmc.dto.WmsStockInOutDto;
import com.yuke.cloud.service.wmc.entity.WmsStockIn;
import com.yuke.cloud.service.wmc.entity.WmsStockInOut;
import com.yuke.cloud.service.wmc.entity.WmsStockOut;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.service.WmsStockInOutService;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * Created by zn on 2020/11/06.
 */
@Service
@Transactional
public class WmsStockInOutServiceImpl extends BaseService<WmsStockInOut> implements WmsStockInOutService {
    @Resource
    private WmsStockInOutMapper wmsStockInOutMapper;
    @Resource
    private WmsStockOutMapper wmsStockOutMapper;
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsStorageController wmsStorageController;
    @Resource
    private WmsStockInMapper wmsStockInMapper;
    @Resource
    private WmsStorageMapper wmsStorageMapper;


    @Override
    public Integer partGoods(WmsStockInOut wmsStockInOut) {
        //被拆商品
        //插入出库记录
        WmsStockOut wmsStockOut = new WmsStockOut();
        wmsStockOut.setSpecId(wmsStockInOut.getOutSpecId());
        wmsStockOut.setProductId(wmsStockInOut.getOutProductId());
        wmsStockOut.setOutPrice(wmsStockInOut.getOutPrice());
        wmsStockOut.setOutNumber(wmsStockInOut.getOutNumber());
        wmsStockOut.setWarehouseId(wmsStockInOut.getWarehouseId());
        wmsStockOut.setProductId(wmsStockInOut.getOutProductId());
        wmsStockOut.setOutType((byte) 5);
        wmsStockOut.setUpdateTime(new Date());
        wmsStockOut.setOutboundTime(new Date());
        wmsStockOut.setIsDelete(false);
        wmsStockOutMapper.insert(wmsStockOut);

        //更新wmsStorage表内库存数
        WmsStorage wmsStorage = new WmsStorage();
        wmsStorage.setSpecId(wmsStockOut.getSpecId());
        wmsStorage.setWarehouseId(wmsStockOut.getWarehouseId());
        List<WmsStorage> select = wmsStorageService.select(wmsStorage);

        wmsStorage = select.get(0);

        Integer number = wmsStorage.getNumber() - wmsStockOut.getOutNumber();
        //只削减库存
        wmsStorage.setNumber(number);
        wmsStorage.setUpdateTime(new Date());

        Integer update = wmsStorageService.update(wmsStorage);



        //
        if (update > 0) {
            //插入入库记录
            WmsStockIn wmsStockIn = new WmsStockIn();
            wmsStockIn.setWarehouseId(wmsStockInOut.getWarehouseId());
            wmsStockIn.setUpdateId(wmsStockInOut.getUpdateId());
            wmsStockIn.setSpecId(wmsStockInOut.getInSpecId());
            wmsStockIn.setInNumber(wmsStockInOut.getInNumber());
            wmsStockIn.setInType((byte) 4);
            wmsStockIn.setProductId(wmsStockInOut.getInProductId());
            wmsStockIn.setInboundTime(new Date());
            wmsStockIn.setUpdateTime(new Date());
            wmsStockIn.setIsDelete(false);
            Integer i = wmsStockInMapper.insert(wmsStockIn);

            //更新WmsStorage
            //根据list 更新wmsStorage表内库存数
            WmsStorage wmsStorageIn = new WmsStorage();
            wmsStorageIn.setSpecId(wmsStockInOut.getInSpecId());
            wmsStorageIn.setWarehouseId(wmsStockInOut.getWarehouseId());
            List<WmsStorage> selectIn = wmsStorageService.select(wmsStorageIn);
            WmsStorage storageIn = selectIn.get(0);
            //商品拆装后进货
            Integer numberIn = storageIn.getNumber() + wmsStockInOut.getInNumber();
            storageIn.setNumber(numberIn);
            storageIn.setUpdateTime(new Date());
            System.out.println("wmsStockIn"+wmsStockIn);
            wmsStorageService.update(storageIn);



            //拆分后 将拆分前拆分后的关联起来
            wmsStockInOut.setStockOutId(wmsStockOut.getStockOutId());
            wmsStockInOut.setStockInId(wmsStockIn.getStockInId());
            System.out.println("+++++++"+"wmsStockInOut"+wmsStockInOut);


            Integer insert = wmsStockInOutMapper.insertSelective(wmsStockInOut);
            if (insert.equals(0)) {
                return 0;
            }
        } else if (update.equals(0)) {
            return 0;
        }
        return 1;
    }



    @Transactional
    @Override
    public Integer deletePartGoods(Long id) {
        WmsStockInOut wmsStockInOut = wmsStockInOutMapper.selectByPrimaryKey(id);
        //出库隐藏
        WmsStockOut wmsStockOut = new WmsStockOut();
        wmsStockOut.setStockOutId(wmsStockInOut.getStockOutId());
        wmsStockOut.setIsDelete(true);
        wmsStockOutMapper.updateByPrimaryKeySelective(wmsStockOut);

        //调整库存
        WmsStorage wmsStorage = new WmsStorage();
        wmsStorage.setSpecId(wmsStockInOut.getOutSpecId());
        wmsStorage.setWarehouseId(wmsStockInOut.getWarehouseId());
        List<WmsStorage> select = wmsStorageService.select(wmsStorage);
        wmsStorage = select.get(0);
        System.out.println("wmsStorage"+wmsStorage);
        System.out.println("wmsStockInOut"+wmsStockInOut);
        Integer out = wmsStorage.getNumber() + wmsStockInOut.getOutNumber();
        wmsStorage.setNumber(out);
        wmsStorage.setUpdateTime(new Date());
        int i = wmsStorageMapper.updateByPrimaryKeySelective(wmsStorage);
        System.out.println("i"+i+"-------------------");

        //入库隐藏
        WmsStockIn wmsStockIn = new WmsStockIn();
        wmsStockIn.setStockInId(wmsStockInOut.getStockInId());
        wmsStockIn.setIsDelete(true);
        wmsStockInMapper.updateByPrimaryKeySelective(wmsStockIn);

        //调整库存 删除入库即加
        WmsStorage wmsStorageIn = new WmsStorage();
        wmsStorageIn.setSpecId(wmsStockInOut.getInSpecId());
        wmsStorageIn.setWarehouseId(wmsStockInOut.getWarehouseId());
        List<WmsStorage> selectIn = wmsStorageService.select(wmsStorageIn);
        wmsStorageIn = selectIn.get(0);
        Integer in = wmsStorageIn.getNumber() - wmsStockInOut.getInNumber();
        wmsStorageIn.setNumber(in);
        wmsStorageIn.setUpdateTime(new Date());
        wmsStorageMapper.updateByPrimaryKeySelective(wmsStorageIn);

        //隐藏中间表
        wmsStockInOut.setIsDelete(true);
        Integer end = wmsStockInOutMapper.updateByPrimaryKeySelective(wmsStockInOut);

        return end;
    }

    @Override
    public List<WmsStockInOutDto> selectAndSpecName(WmsStockInOutDto wmsStockInOutDto) {

        List<WmsStockInOutDto> list = wmsStockInOutMapper.selectAndSpecName(wmsStockInOutDto);

        return list;
    }


}
